<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.util.Date" %>
<%@ page import="ut.data.User" %>
<%@ page import="ut.data.Profile" %>
<%@ page import="ut.data.FreeTask" %>
<%@ page import="ut.res.Language" %>
<%@ page import="ut.util.DateTime" %>
<%
	User user = (User)session.getAttribute("user");
	long tid = 0;
	int p = 1;
	if(request.getParameter("tid") == null){
		response.sendRedirect("/free-task/free-task-list.jsp");
		return;
	}
	try{
		tid = Long.parseLong(request.getParameter("tid"));
		if(request.getParameter("p") != null){
			p = Integer.parseInt(request.getParameter("p"));
		}
	}catch(NumberFormatException e){
		response.sendRedirect("/free-task/free-task-list.jsp");
		return;
	}
	
	FreeTask task = FreeTask.getFreeTask(tid);
	if(task == null){
		response.sendRedirect("/free-task/free-task-list.jsp");
		return;
	}
	int maxPage = ((task.responseNumber - 1) / 10) + 1;
	if(p > maxPage){
		p = maxPage;
	}
	
	String lang = 
		Language.getLanguage(task.fromLan).title + " <img src='/image/arrow.png' /> " + Language.getLanguage(task.toLan).title;
%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>UT終極一翻！ | 免費案件#<%= tid %></title>
	<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
	<link rel="stylesheet" type="text/css" href="/css/task-detail.css" media="all" />
	<script type="text/javascript" src="/js/Utility.js"></script>
	<script type="text/javascript" src="/js/Message.js"></script>
	<script type="text/javascript" src="/js/Ajax.js"></script>
	<script type="text/javascript" src="/js/Button.js"></script>
	<script type="text/javascript" src="/js/Account.js"></script>
	<script type="text/javascript" src="/js/FreeTask.js"></script>
	<script type="text/javascript" src="/script.js"></script>
</head>
<body onload="init();">
<%@ include file="/include/banner.jsp" %>
	<div class="content">
		<div class="title">免費案件#<%= tid %></div>
		<div class="task block">
			<span class="tag blue top"><%= lang %></span>
			<div class="task-content">
				<div class="task-element">
					<div class="task-element-left">案主：</div>
					<div class="task-element-right">
						<a href="/account/member-info.jsp?uid=<%= task.employer %>&view=1"><%= User.getUserName(task.employer) %></a>
					</div>
				</div>
				<div class="task-element">
					<div class="task-element-left">發案日期：</div>
					<div class="task-element-right">
						<%= DateTime.toDateTime(task.publishTime) %>
					</div>
				</div>
				<div class="task-element">
					<div class="task-element-left">截止日期：</div>
					<div class="task-element-right">
						<%= DateTime.toDateTime(task.finishTime) %>
					</div>
				</div>
				<div class="task-element">
					<div class="task-element-left">翻譯原文：</div>
					<div class="task-element-right">
						<div class='block-content'><%= task.content %></div>
					</div>
				</div>
			</div>
			<div class="task-bottom">
				<span class="tag blue bottom">譯文數 <span class="mark-red"><%= task.responseNumber %></span></span>
			</div>
		</div>
<%
	if(task.finishTime.before(new Date())){	//已結案案件
		FreeTask.Response pickedResponse = FreeTask.getPickedResponse(tid, task.translator);
		//列出最佳翻譯
		if(pickedResponse != null){
%>
		<div class="task block">
			<span class="tag top">
				最佳翻譯
				<a href="/account/member-info.jsp?uid=<%= pickedResponse.translator %>&view=2"><%= User.getUserName(pickedResponse.translator) %></a>
			</span>
			<div class="task-content block-content"><%= pickedResponse.content %></div>
			<div class="task-bottom">
				<span class="tag bottom"><%= DateTime.toDateTime(pickedResponse.publishTime) %></span>
			</div>
		</div>
<%
		}
%>
		<div class="page">
<%
		StringBuilder pageOptions = new StringBuilder("");
		if(p > 1){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=1\">第一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p-1) + "\">上一頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">第一頁</span><span class=\"page-link inactive\">上一頁</span>");
		}
		int startPage = (p - 2 > 0) ? p - 2 : 1;
		int endPage = startPage + 4;
		if(endPage > maxPage){
			startPage = startPage - (endPage - maxPage);
			endPage = maxPage;
		}
		for(int i = startPage; i <= endPage; i++){
			if(i < 1){
				continue;
			}else if(i == p){
				pageOptions.append("<span class=\"page-link current\">" + i + "</span>");
			}else{
				pageOptions.append("<a class=\"page-link\" href=\"./?page=" + i + "\">" + i + "</a>");
			}
		}
		if(p < maxPage){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p+1) + "\">下一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + maxPage + "\">最後頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">下一頁</span><span class=\"page-link inactive\">最後頁</span>");
		}
		out.println(pageOptions.toString());
%>
		</div>
<%
		//列出其他翻譯
		FreeTask.Response[] resps = FreeTask.getResponses(tid, 10, p);
		for(int i = 0; i < resps.length; i++){
			if(resps[i] == null){
				if(i == 0 || i == 1){
%>
		<div class="task block">
			<div class="block-center">沒有其他翻譯</div>
		</div>
<%
				}
				break;
			}
			if(task.translator == resps[i].translator){
				continue;
			}
%>
		<div class="task block">
			<span class="tag top green">
				譯者
				<a href="/account/member-info.jsp?uid=<%= resps[i].translator %>&view=2"><%= User.getUserName(resps[i].translator) %></a>
			</span>
			<div class="task-content block-content"><%= resps[i].content %></div>
			<div class="task-bottom">
				<span class="tag bottom green"><%= DateTime.toDateTime(resps[i].publishTime) %></span>
			</div>
		</div>
<%
		}
%>
		<div class="page">
<%		
		out.println(pageOptions.toString());
%>
		</div>
<%
	}else if(user != null && user.id == task.employer){	// 案主
%>
		<div class="page">
<%
		StringBuilder pageOptions = new StringBuilder("");
		if(p > 1){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=1\">第一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p-1) + "\">上一頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">第一頁</span><span class=\"page-link inactive\">上一頁</span>");
		}
		int startPage = (p - 2 > 0) ? p - 2 : 1;
		int endPage = startPage + 4;
		if(endPage > maxPage){
			startPage = startPage - (endPage - maxPage);
			endPage = maxPage;
		}
		for(int i = startPage; i <= endPage; i++){
			if(i < 1){
				continue;
			}else if(i == p){
				pageOptions.append("<span class=\"page-link current\">" + i + "</span>");
			}else{
				pageOptions.append("<a class=\"page-link\" href=\"./?page=" + i + "\">" + i + "</a>");
			}
		}
		if(p < maxPage){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p+1) + "\">下一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + maxPage + "\">最後頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">下一頁</span><span class=\"page-link inactive\">最後頁</span>");
		}
		out.println(pageOptions.toString());
%>
		</div>
<%
		FreeTask.Response[] resps = FreeTask.getResponses(tid, 10, p);
		for(int i = 0; i < resps.length; i++){
			if(resps[i] == null){
				if(i == 0){
%>
		<div class="task block">
			<div class="block-center">沒有其他翻譯</div>
		</div>
<%
				}
				break;
			}
%>
		<div class="task block">
			<span class="tag top green">譯文 #<%= (i + 1) %></span>
			<div class="task-content">
				<div class="block-content"><%= resps[i].content %></div>
				<div class="task-element" style="margin-top:10px">
					<div class="task-element-left">譯者：</div>
					<div class="task-element-right">
						<a href="/account/member-info.jsp?uid=<%= resps[i].translator %>&view=2"><%= User.getUserName(resps[i].translator) %></a>
					</div>
					<div style="clear:both"></div>
				</div>
				<div class="task-element">
					<div class="task-element-left">翻譯日期：</div>
					<div class="task-element-right">
						<%= DateTime.toDateTime(resps[i].publishTime) %>
					</div>
				</div>
			</div>
			<div style="clear:both"></div>
			<div class="task-bottom">
				<span class="tag bottom green tool"
					onclick="FreeTask.pickResponse(<%= resps[i].id %>, <%= resps[i].translator %>)">
					選為最佳翻譯
				</span>
			</div>
		</div>
<%
		}
%>
		<div class="page">
<%		
		out.println(pageOptions.toString());
%>
		</div>
<%
	}else if(user == null){	//訪客
%>
		<div class="task block">
			<span class="tag top green">我要翻譯</span>
			<form class="resp" onsubmit="FreeTask.response(this);return false;">
				<textarea disabled name="content"></textarea>
				<div>
					<input disabled type="submit" class="button green" value="請先登入以翻譯案件"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
				<input type="hidden" name="tid" value="<%= tid %>" />
			</form>
		</div>
<%
	}else if(user.verify == 0){	//未驗證E-Mail
%>
		<div class="task block">
			<span class="tag top green">我要翻譯</span>
			<form class="resp" onsubmit="FreeTask.response(this);return false;">
				<textarea disabled name="content"></textarea>
				<div>
					<input disabled type="submit" class="button green" value="請先通過E-Mail驗證以翻譯案件"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
				<input type="hidden" name="tid" value="<%= tid %>" />
			</form>
		</div>
<%
	}else if(Profile.getProfile(user.id) == null){	//尚未建立譯者資料
%>
		<div class="task block">
			<span class="tag top green">我要翻譯</span>
			<form class="resp" onsubmit="FreeTask.response(this);return false;">
				<textarea disabled="disabled" name="content"></textarea>
				<div>
					<input disabled="disabled" type="submit" class="button green" value="請先建立譯者資料以翻譯案件"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
				<input type="hidden" name="tid" value="<%= tid %>" />
			</form>
		</div>
<%
	}else if(FreeTask.checkResponsed(task.id, user.id)){	//已翻譯過此案件
%>
		<div class="task block">
			<span class="tag top green">我要翻譯</span>
			<form class="resp" onsubmit="FreeTask.response(this);return false;">
				<textarea disabled="disabled" name="content"></textarea>
				<div>
					<input disabled="disabled" type="submit" class="button green" value="您已翻譯過此案件"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
				<input type="hidden" name="tid" value="<%= tid %>" />
			</form>
		</div>
<%
	}else{	//可以翻譯
%>
		<div class="task block">
			<span class="tag top green">我要翻譯</span>
			<form class="resp" onsubmit="FreeTask.response(this);return false;">
				<textarea name="content"></textarea>
				<div>
					<input type="submit" class="button green" value="送出"
						onmousedown="this.style.borderStyle='inset';"
						onmouseup="this.style.borderStyle='outset';"
						onmouseout="this.style.borderStyle='outset';" />
				</div>
				<input type="hidden" name="tid" value="<%= tid %>" />
			</form>
		</div>
<%
	}
%>
<%@ include file="/include/foot.jsp" %>
	</div>
</body>
</html>